#include "FPPCamera.h"


FPPCamera::FPPCamera()
{
	m_Up= D3DXVECTOR3(0,1,0);
	m_FromEye = D3DXVECTOR3(0,0,-1);
	m_At= D3DXVECTOR3(0,2,0);
	m_Radius=30.0f;
	D3DXQuaternionIdentity(&m_Rotation);



}

D3DXMATRIX FPPCamera::getViewMatrix()
{
	D3DXVECTOR4 TmpFromEye;
	D3DXVECTOR4 TmpUp;
	D3DXMATRIX  TmpMatrix;
	D3DXMATRIX  View;

	D3DXMatrixRotationQuaternion(&TmpMatrix,&m_Rotation);
	D3DXVec3Transform(&TmpFromEye, &m_FromEye, &TmpMatrix);
	D3DXVec3Transform(&TmpUp, &m_Up, &TmpMatrix);

	D3DXVECTOR3 TargetPos=m_At+(D3DXVECTOR3(TmpFromEye.x,TmpFromEye.y,TmpFromEye.z)*m_Radius);
	D3DXVECTOR3 Up(TmpUp.x,TmpUp.y,TmpUp.z);



	D3DXMatrixLookAtLH(&View, 
		&m_At, 
		&TargetPos, 
		&Up);

	return View;
}

D3DXVECTOR3 FPPCamera::getEyePosition()
{
	return m_At;
}

void FPPCamera::move(D3DXVECTOR3 v)
{
	D3DXQUATERNION TempRot;
	D3DXMATRIX  TmpMatrix;
	D3DXVECTOR4 TmpVec;

	D3DXMatrixRotationQuaternion(&TmpMatrix,&m_Rotation);
	D3DXVec3Transform(&TmpVec, &v, &TmpMatrix);
	D3DXVECTOR3 vec=D3DXVECTOR3(TmpVec.x,0,TmpVec.z);



	m_At+=vec;
}

void FPPCamera::RotateY(float angle)
{
	D3DXQUATERNION TempRot;
	D3DXVECTOR3 Axis(0,1,0);


	D3DXQuaternionRotationAxis(&TempRot,&Axis,angle);
	D3DXQuaternionMultiply(&m_Rotation,&m_Rotation,&TempRot);

}

void FPPCamera::RotateX(float angle)
{
	D3DXQUATERNION TempRot;
	D3DXVECTOR3 Axis(1,0,0);
	D3DXVECTOR4 TmpAxis;
	D3DXMATRIX  TmpMatrix;

	D3DXMatrixRotationQuaternion(&TmpMatrix,&m_Rotation);
	D3DXVec3Transform(&TmpAxis, &Axis, &TmpMatrix);
	Axis=D3DXVECTOR3(TmpAxis.x,TmpAxis.y,TmpAxis.z);

	D3DXQuaternionRotationAxis(&TempRot,&Axis,angle);
	D3DXQuaternionMultiply(&m_Rotation,&m_Rotation,&TempRot);

}



D3DXVECTOR3 FPPCamera::GetAtPosition()
{
	return m_At;
}

void FPPCamera::SetAtPosition(D3DXVECTOR3 position)
{
	m_At=position;
}

D3DXVECTOR3 FPPCamera::getEyeDirection()
{
	D3DXMATRIX  TmpMatrix;
	D3DXVECTOR4 TmpVec;

	D3DXMatrixRotationQuaternion(&TmpMatrix,&m_Rotation);
	D3DXVec3Transform(&TmpVec, &m_FromEye, &TmpMatrix);
	D3DXVECTOR3 vec=D3DXVECTOR3(TmpVec.x,TmpVec.y,TmpVec.z);
	return vec;
}